import React, { useEffect, useState } from "react";
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
} from "@ant-design/icons";
import { Button, Layout, Menu, theme } from "antd";
import Home from "./child/home";
import Shoplist from "./child/shoplist";
import Typelist from "./child/typelist";
import { Switch, Route, Redirect } from "react-router-dom";
import { useAppDispatch, useAppSelector } from "../store/AppUse";
import {store} from '../store'
import * as actionfn from '../store/crmSlice' 
.

const { Header, Sider, Content } = Layout;

const Admin: React.FC = (props) => {

  const obj = useAppSelector(state=>state.todos)
  const dispatch = useAppDispatch()
  useEffect(()=>{
    store.dispatch(actionfn.async_getshoplist(obj))
  },[])

  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  return (
    <Layout>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        
        <Menu
          theme="dark"
          mode="inline"
          defaultOpenKeys={['',props.location.pathname]}
          defaultSelectedKeys={[props.location.pathname]}
          items={[
            {
              key: "/admin/home",
              icon: <UserOutlined />,
              label: "首页",
            },
            {
              key: "",
              icon: <VideoCameraOutlined />,
              label: "电子券管理",
              children: [
                {
                  key: "/admin/shoplist",
                  icon: <UploadOutlined />,
                  label: "优惠券制作",
                },
                {
                  key: "/admin/typelist",
                  icon: <UploadOutlined />,
                  label: "已发布优惠券",
                },
              ],
            },
          ]}
          onSelect={(obj)=>{
            props.history.push(obj.key)
          }}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }}>
          <Button
            type="text"
            icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
            onClick={() => setCollapsed(!collapsed)}
            style={{
              fontSize: "16px",
              width: 64,
              height: 64,
            }}
          />
        </Header>
        <Content
          style={{
            margin: "24px 16px",
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          <Switch>
            <Route path="/admin/home" component={Home} />
            <Route path="/admin/shoplist" component={Shoplist} />
            <Route path="/admin/typelist" component={Typelist} />
            <Redirect from="/admin" to="/admin/home" />
          </Switch>
        </Content>
      </Layout>
    </Layout>
  );
};

export default Admin;
